<template>
	<view style='padding:10px;'>
		<view style='color:#909399'>
			请上传您在本商场的消费小票以及付款凭证，核查无误后，48小时内积分到账。
			<view style='color:#dd6161'>美团、抖音，大众点评以及预充值款项均不计入积分</view>
		</view>
		<u--form labelPosition="left" labelWidth='80px'>
			<u-form-item label="小票">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="10"></u-upload>
			</u-form-item>
			<!-- <u-form-item label="消费金额">
				<u--input v-model="amount"></u--input>
			</u-form-item> -->
		</u--form>
		<view style='margin-top:30px'>
			<u-button text='提交' type='primary' shape='circle' size='large' @click='submit'></u-button>
		</view>
		<view style='text-align: center;color:#909399;padding:10px' @click='toUrl("/pages/mine/self-log")'>
			查看积分进度
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				amount: 0,
				imageUrls: [],
				fileList1: []
			};
		},
		onShow() {

		},
		onLoad() {

		},
		methods: {
			toUrl(page){
				uni.navigateTo({
					url: page
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.imageUrl = ''
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${getApp().globalData.apiBase}upload?storage=qiniu`, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
								console.log(123,res.data)
								let imageInfo = JSON.parse(res.data)
								console.log(imageInfo)
								this.imageUrls.push(imageInfo.data[0]) 
							}, 1000)
						}
					});
				})
			},
			submit(){
				console.log(this.fileList1)
				// 保存积分记录
				if(this.imageUrls && this.imageUrls.length > 0){
					this.request.post(`api/point-applies`,{
						point: 0,
						images: this.imageUrls
					}).then((res) => {
						uni.showModal({
							title: '',
							content: '提交成功！',
							showCancel: false,
							success() {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}
						})
					})
				}else{
					uni.showToast({
						title: '请上传小票图片！',
						icon: 'none'
					})
					return
				}
				
			}
		}
	}
</script>

<style>

</style>